package ro.imc.ums.dani.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;

import ro.imc.ums.dani.classes.Courses;
import ro.imc.ums.dani.classes.Groups;
import ro.imc.ums.dani.classes.Professor;
import ro.imc.ums.dani.classes.Student;
import ro.imc.ums.dani.database.CourseCrud;
import ro.imc.ums.dani.database.GroupCrud;
import ro.imc.ums.dani.database.ProfessorCrud;
import ro.imc.ums.dani.database.StudentCrud;

public class AdminInterface {

	private JFrame window;
	private JPanel panel;
	private JButton exit;
	private JButton nuke;

	private JButton addStudents;
	private JButton addProfessors;
	private JButton addGroups;
	private JButton addCourses;

	private JButton listStudents;
	private JButton listProfessors;
	private JButton listGroups;
	private JButton listCourses;

	/*
	 * Other Windows Variables
	 */
	private JFrame otherWindow;
	private JPanel otherPanel;
	private JTextField firstname;
	private JTextField lastname;
	private JTextField cnp;
	private JTextField id;
	private JTextField number;
	private JTextField title;
	private JTextField field;
	private JTextField groupName;
	private JTextField groupYear;
	private JTextField groupFieldOfStudy;
	private JTextField courseName;
	private JTextField courseId;
	private JTextField courseSemester;
	private JComboBox status;
	private JComboBox courseMandatory;
	private boolean accountStatus = true;
	private boolean courseStatus = true;
	private JButton add;
	private JButton cancel;
	private JButton update;
	private JButton delete;
	
	private JList listBox;

	public AdminInterface() {
		createInterface();
		createPanel();
		createButtons();
		window.setVisible(true);
	}

	private void createInterface() {
		window = new JFrame();
		window.setTitle("UMS - Admin Interface");
		window.setSize(500, 400);
		window.setLocation(300, 300);
		window.setResizable(false);
		window.setLayout(new BorderLayout());
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}

	private void createPanel() {
		panel = new JPanel();
		panel.setLayout(null);
		panel.setBackground(Color.WHITE);
		window.add(panel);
	}

	private void createButtons() {
		exit = new JButton("Exit");
		exit.setSize(85, 25);
		exit.setLocation(360, 325);
		exit.setForeground(Color.black);
		exit.setBackground(Color.red);

		ActionListener actionExit = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		};
		exit.addActionListener(actionExit);
		
		
		/*
		 * Add Buttons
		 */
		addStudents = new JButton("Add Students");
		addStudents.setSize(130, 25);
		addStudents.setLocation(45, 50);
		addStudents.setBackground(Color.cyan);
		ActionListener actionAddStudents = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				createStudentsWindow();
			}

		};
		addStudents.addActionListener(actionAddStudents);

		addProfessors = new JButton("Add Professors");
		addProfessors.setSize(130, 25);
		addProfessors.setLocation(45, 85);
		addProfessors.setBackground(Color.cyan);
		ActionListener actionAddProfessors = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				createProfessorsWindow();
			}			
		};
		addProfessors.addActionListener(actionAddProfessors);
		
		addGroups = new JButton("Add Groups");
		addGroups.setSize(130, 25);
		addGroups.setLocation(45, 120);
		addGroups.setBackground(Color.cyan);
		ActionListener actionAddGroups = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				createGroupsWindow();
			}			
		};
		addGroups.addActionListener(actionAddGroups);
		
		addCourses = new JButton("Add Courses");
		addCourses.setSize(130, 25);
		addCourses.setLocation(45, 155);
		addCourses.setBackground(Color.cyan);
		ActionListener actionAddCourses = new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				createCoursesWindow();
			}			
		};
		addCourses.addActionListener(actionAddCourses);
		
		/*
		 * List Buttons
		 */
		listStudents = new JButton("List Students");
		listStudents.setSize(130, 25);
		listStudents.setLocation(295, 50);
		listStudents.setBackground(Color.cyan);
		ActionListener actionListStudents = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				listStudentsWindow();
			}

		};
		listStudents.addActionListener(actionListStudents);

		listProfessors = new JButton("List Professors");
		listProfessors.setSize(130, 25);
		listProfessors.setLocation(295, 85);
		listProfessors.setBackground(Color.cyan);
		ActionListener actionListProfessors = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				listProfessorsWindow();
			}
		};
		listProfessors.addActionListener(actionListProfessors);
		
		listGroups = new JButton("List Groups");
		listGroups.setSize(130, 25);
		listGroups.setLocation(295, 120);
		listGroups.setBackground(Color.cyan);
		ActionListener actionListGroups = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				listGroupsWindow();
			}
		};
		listGroups.addActionListener(actionListGroups);
		
		listCourses = new JButton("List Courses");
		listCourses.setSize(130, 25);
		listCourses.setLocation(295, 155);
		listCourses.setBackground(Color.cyan);
		ActionListener actionListCourses = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(false);
				listCoursesWindow();
			}
		};
		listCourses.addActionListener(actionListCourses);
		
		nuke = new JButton("Nuke Database");
		nuke.setSize(130, 25);
		nuke.setLocation(170, 250);
		nuke.setForeground(Color.black);
		nuke.setBackground(Color.red);

		panel.add(nuke);
		panel.add(listStudents);
		panel.add(listProfessors);
		panel.add(listGroups);
		panel.add(listCourses);
		panel.add(addStudents);
		panel.add(addProfessors);
		panel.add(addGroups);
		panel.add(addCourses);
		panel.add(exit);
	}

	/*
	 * Add Students Window
	 */
	private void createStudentsWindow() {
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Add Students");
		otherWindow.setSize(350, 130);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new GridLayout(0, 2));
		otherPanel.setBackground(Color.WHITE);

		firstname = new JTextField("First Name");
		lastname = new JTextField("Last Name");
		cnp = new JTextField("Student Cnp");
		id = new JTextField("Student Id");
		number = new JTextField("Student Number");

		String[] index = { "Active", "Inactive" };
		status = new JComboBox(index);

		ActionListener actionStatus = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox) e.getSource();
				String x = (String) cb.getSelectedItem();
				if (x == "Active") {
					accountStatus = true;
				} else if (x == "Inactive") {
					accountStatus = false;
				}
			}
		};
		status.addActionListener(actionStatus);

		add = new JButton("Add");
		add.setBackground(Color.green);
		add.setForeground(Color.black);
		
		ActionListener actionAdd = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String studentFirstName = firstname.getText();
				String studentLastName = lastname.getText();
				String studentCnp = cnp.getText();
				int studentId = Integer.parseInt(id.getText());
				int studentNumber = Integer.parseInt(number.getText());
				boolean studentStatus = accountStatus;

				Student studentInfo = new Student(studentFirstName,
						studentLastName, studentId, studentCnp, studentNumber,
						studentStatus);
				StudentCrud studentInsert = new StudentCrud();
				studentInsert.insert(studentInfo);
			}
		};

		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		ActionListener actionCancel = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(true);
				otherWindow.dispose();
			}
		};

		cancel.addActionListener(actionCancel);
		add.addActionListener(actionAdd);
		otherWindow.add(otherPanel);
		otherPanel.add(firstname);
		otherPanel.add(lastname);
		otherPanel.add(cnp);
		otherPanel.add(id);
		otherPanel.add(number);
		otherPanel.add(status);
		otherPanel.add(add);
		otherPanel.add(cancel);
	}
	
	/*
	 * List Students Window
	 */
	private void listStudentsWindow(){
		
		List<Object> studentList = new ArrayList<Object>();
		StudentCrud  getStudentList = new StudentCrud();
		studentList = (ArrayList<Object>)getStudentList.select();
		
		List<Student> studentListInfo = new ArrayList<Student>();
		for(Object o : studentList){
			studentListInfo.add((Student)o);
		}
	
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Current Students");
		otherWindow.setSize(350, 200);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new BorderLayout());
		otherPanel.setBackground(Color.WHITE);
		
		
		String[] dataInfo = new String[studentListInfo.size()];
		for(int i=0;i<studentListInfo.size();i++){			
			dataInfo[i] = studentListInfo.get(i).getFirstName() + " " + studentListInfo.get(i).getLastName() + "\n";			
		}
		
		listBox = new JList(dataInfo);
		listBox.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listBox.setLayoutOrientation(JList.HORIZONTAL_WRAP);
				
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		
		otherWindow.add(otherPanel);		
		
		//otherPanel.add(cancel);
		otherPanel.add(listBox, BorderLayout.CENTER);
		
		
		
	}
	
	/*
	 * Add Professors Window
	 */
	private void createProfessorsWindow(){
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Add Professors");
		otherWindow.setSize(350, 130);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new GridLayout(0, 2));
		otherPanel.setBackground(Color.WHITE);
	
		firstname = new JTextField("First Name");
		lastname = new JTextField("Last Name");
		cnp = new JTextField("Professor Cnp");
		id = new JTextField("Profesor Id");
		title = new JTextField("Title");
		field = new JTextField("Field");
		
		add = new JButton("Add");
		add.setBackground(Color.green);
		add.setForeground(Color.black);
		ActionListener actionAdd = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String profesorFirstName = firstname.getText();
				String profesorLastName = lastname.getText();
				String profesorCnp = cnp.getText();
				int profesorId = Integer.parseInt(id.getText());
				String professorTitle = title.getText();
				String professorField = field.getText();

				Professor professorInfo = new Professor(profesorFirstName,
						profesorLastName, profesorId, profesorCnp, professorTitle,
						professorField);
				ProfessorCrud studentInsert = new ProfessorCrud();
				studentInsert.insert(professorInfo);
			}
		};
		
		
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		ActionListener actionCancel = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(true);
				otherWindow.dispose();
			}
		};
		
		cancel.addActionListener(actionCancel);
		add.addActionListener(actionAdd);
		otherWindow.add(otherPanel);
		otherPanel.add(firstname);
		otherPanel.add(lastname);
		otherPanel.add(cnp);
		otherPanel.add(id);
		otherPanel.add(title);
		otherPanel.add(field);
		otherPanel.add(add);
		otherPanel.add(cancel);
	
	}
	
	/*
	 * List Professors Window
	 */
	private void listProfessorsWindow(){
		List<Object> professorsList = new ArrayList<Object>();
		ProfessorCrud  getProfessorsList = new ProfessorCrud();
		professorsList = (ArrayList<Object>)getProfessorsList.select();
		
		List<Professor> professorListInfo = new ArrayList<Professor>();
		for(Object o : professorsList){
			professorListInfo.add((Professor)o);
		}
		
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Current Professors");
		otherWindow.setSize(350, 130);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new GridLayout(0, 2));
		otherPanel.setBackground(Color.WHITE);
	
		String[] dataInfo = new String[professorListInfo.size()];
		for(int i=0;i<professorListInfo.size();i++){			
			dataInfo[i] = professorListInfo.get(i).getFirstName() + " " + professorListInfo.get(i).getLastName() + "\n";			
		}
		
		listBox = new JList(dataInfo);
		listBox.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listBox.setLayoutOrientation(JList.HORIZONTAL_WRAP);
				
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		
		otherWindow.add(otherPanel);		
		
		//otherPanel.add(cancel);
		otherPanel.add(listBox, BorderLayout.CENTER);
	}
	
	/*
	 * Add Groups Window
	 */
	private void createGroupsWindow(){
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Add Groups");
		otherWindow.setSize(350, 130);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new GridLayout(0, 2));
		otherPanel.setBackground(Color.WHITE);
	
		groupName = new JTextField("Name");
		groupYear = new JTextField("Year");
		groupFieldOfStudy = new JTextField("Field of Study");
		
		
		add = new JButton("Add");
		add.setBackground(Color.green);
		add.setForeground(Color.black);
		ActionListener actionAdd = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String gName = groupName.getText();
				String gFieldOfStudy = groupFieldOfStudy.getText();
				int gYear = Integer.parseInt(groupYear.getText());
				

				Groups groupsInfo = new Groups(gName,
						gYear, gFieldOfStudy);
				GroupCrud groupInsert = new GroupCrud();
				groupInsert.insert(groupsInfo);
			}
		};
		
		
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		ActionListener actionCancel = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(true);
				otherWindow.dispose();
			}
		};
		
		cancel.addActionListener(actionCancel);
		add.addActionListener(actionAdd);
		otherWindow.add(otherPanel);
		otherPanel.add(groupName);
		otherPanel.add(groupYear);
		otherPanel.add(groupFieldOfStudy);
		otherPanel.add(add);
		otherPanel.add(cancel);
	
	}
	
	/*
	 * List Groups Window
	 */
	private void listGroupsWindow(){
		List<Object> groupList = new ArrayList<Object>();
		GroupCrud  getGroupList = new GroupCrud();
		groupList = (ArrayList<Object>)getGroupList.select();
		
		List<Groups> groupListInfo = new ArrayList<Groups>();
		for(Object o : groupList){
			groupListInfo.add((Groups)o);
		}
	
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Current Groups");
		otherWindow.setSize(350, 200);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new BorderLayout());
		otherPanel.setBackground(Color.WHITE);
		
		
		String[] dataInfo = new String[groupListInfo.size()];
		for(int i=0;i<groupListInfo.size();i++){			
			dataInfo[i] = groupListInfo.get(i).getName() + " " + groupListInfo.get(i).getFieldOfStudy() + "\n";			
		}
		
		listBox = new JList(dataInfo);
		listBox.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listBox.setLayoutOrientation(JList.HORIZONTAL_WRAP);
				
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		
		otherWindow.add(otherPanel);		
		
		//otherPanel.add(cancel);
		otherPanel.add(listBox, BorderLayout.CENTER);
		
	}
	
	/*
	 * Add Courses Window
	 */
	private void createCoursesWindow(){
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Add Courses");
		otherWindow.setSize(350, 130);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new GridLayout(0, 2));
		otherPanel.setBackground(Color.WHITE);
	
		courseName = new JTextField("Course Name");		
		courseId = new JTextField("Course Id");
		courseSemester = new JTextField("Semester");
		String[] index = { "Mandatory", "Not Mandatory" };
		courseMandatory = new JComboBox(index);

		ActionListener actionStatus = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JComboBox cb = (JComboBox) e.getSource();
				String x = (String) cb.getSelectedItem();
				if (x == "Mandatory") {
					courseStatus = true;
				} else if (x == "Not Mandatory") {
					courseStatus = false;
				}
			}
		};
		
		add = new JButton("Add");
		add.setBackground(Color.green);
		add.setForeground(Color.black);
		ActionListener actionAdd = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String cName = courseName.getText();
				boolean cMandatory = courseStatus;
				int cId = Integer.parseInt(courseId.getText());
				int cSemester = Integer.parseInt(courseSemester.getText());
				

				Courses courseInfo = new Courses(cName,
						cMandatory, cId, cSemester);
				CourseCrud courseInsert = new CourseCrud();
				courseInsert.insert(courseInfo);
			}
		};
		
		
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		ActionListener actionCancel = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				window.setVisible(true);
				otherWindow.dispose();
			}
		};
		
		cancel.addActionListener(actionCancel);
		add.addActionListener(actionAdd);
		otherWindow.add(otherPanel);
		otherPanel.add(courseName);
		otherPanel.add(courseMandatory);
		otherPanel.add(courseId);
		otherPanel.add(courseSemester);
		otherPanel.add(add);
		otherPanel.add(cancel);
	
	}
	
	
	/*
	 * List Courses Window
	 */
	private void listCoursesWindow(){
		List<Object> courseList = new ArrayList<Object>();
		CourseCrud  getCourseList = new CourseCrud();
		courseList = (ArrayList<Object>)getCourseList.select();
		
		List<Courses> courseListInfo = new ArrayList<Courses>();
		for(Object o : courseList){
			courseListInfo.add((Courses)o);
		}
	
		otherWindow = new JFrame();
		otherWindow.setTitle("UMS - Current Courses");
		otherWindow.setSize(350, 200);
		otherWindow.setLocation(350, 350);
		otherWindow.setResizable(false);
		otherWindow.setVisible(true);

		otherPanel = new JPanel();
		otherPanel.setLayout(new BorderLayout());
		otherPanel.setBackground(Color.WHITE);
		
		
		String[] dataInfo = new String[courseListInfo.size()];
		for(int i=0;i<courseListInfo.size();i++){			
			dataInfo[i] = courseListInfo.get(i).getCourseName() + " " + courseListInfo.get(i).getSemester() + "\n";			
		}
		
		listBox = new JList(dataInfo);
		listBox.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		listBox.setLayoutOrientation(JList.HORIZONTAL_WRAP);
				
		cancel = new JButton("Cancel");
		cancel.setBackground(Color.red);
		cancel.setForeground(Color.black);
		
		otherWindow.add(otherPanel);		
		
		//otherPanel.add(cancel);
		otherPanel.add(listBox, BorderLayout.CENTER);
	}
}
